package com.yunjijia.controller;


import com.yunjijia.pojo.Built;
import com.yunjijia.service.BuiltService;
import com.yunjijia.utils.PageResult;
import com.yunjijia.utils.R;
import com.yunjijia.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/built")
public class BuiltController {

    @Resource
    private BuiltService builtService;

    /**
     * 保存 / 修改
     *
     * @param built
     * @return
     */
    @RequestMapping(value = "/save")
    public R save(@RequestBody Built built) {
        if (null != built.getId()) {
            builtService.update(built);
            return new R(true, StatusCode.OK, "修改成功");
        } else {
            builtService.save(built);
            return new R(true, StatusCode.OK, "添加成功");
        }

    }


    /**
     * 删除 /将状态改为下架
     *
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public R del(long id) {
        builtService.del(id);
        return new R(true, StatusCode.OK, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delS")
    public R delS(long[] ids) {
        builtService.dels(ids);
        return new R(true, StatusCode.OK, "删除成功");
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @RequestMapping("/select")
    public R selectById(long id) {
        Built built = builtService.selectById(id);
        return new R(true, StatusCode.OK, "查询成功", built);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping("/selectAll")
    public R selectAll() {
        List<Built> list = builtService.selectAll();
        return new R(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/search")
    public PageResult search(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return builtService.page(pageNum, pageSize);
    }

    /**
     * 条件分页查询
     * @param page
     * @param size
     * @param built
     * @return
     */
    @RequestMapping("/requirementSearch")
    public PageResult requirementSearch(int page,int size, @RequestBody Built built)
    {
                 return builtService.requirementSearch(page,size,built);
    }

}